const Router = require('koa-router')

const router = new Router({ prefix: '/coupon' })

// 引入数据库方法
const service = require('../service/coupon_service')

// 获取优惠券
router.get('/getcoupon', async (ctx, next) => {
  const userId = ctx.request.query.userId
  const result = await service.getCoupon(userId)
  ctx.body = result
})

// 兑换优惠券
router.post('/exchange', async (ctx, next) => {
  const CouponsData = ctx.request.body
  const { status, message } = await service.exchangeCoupon(CouponsData)
  ctx.status = status
  ctx.body = message
})

// 删除已经使用过的优惠券
router.post('/delete', async (ctx, next) => {
  const deleteInfo = ctx.request.body
  const { status, message } = await service.deleteCoupon(deleteInfo)
  ctx.status = status
  ctx.body = message
})

module.exports = router